package com.coffee.sku.service.inquire.impl;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.service.platform.bean.PlatformBaseDeploy;
import com.coffee.admin.service.platform.dao.PlatformBaseDeployDao;
import com.coffee.admin.service.supplier.bean.Supplier;
import com.coffee.admin.service.supplier.dao.SupplierDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.enums.SkuTypeEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.exception.ValidateBusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.BeanUtils;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.sku.api.inquire.query.InquireSalesInformationQuery;
import com.coffee.sku.api.inquire.query.InquireSkuContentQuery;
import com.coffee.sku.api.inquire.query.InquireSkuTestingMessageQuery;
import com.coffee.sku.api.inquire.rep.InquireSkuListRep;
import com.coffee.sku.api.inquire.req.InquireSalesInformationReq;
import com.coffee.sku.api.inquire.req.InquireSkuAddReq;
import com.coffee.sku.api.inquire.req.InquireSkuTestingMessageReq;
import com.coffee.sku.api.inquire.req.InquireSkuUpdateReq;
import com.coffee.sku.api.inquire.result.InquireSalesInformationResult;
import com.coffee.sku.api.inquire.result.InquireSkuContentResult;
import com.coffee.sku.api.inquire.result.InquireSkuTestingMessageResult;
import com.coffee.sku.api.inquire.service.InquireSkuContentService;
import com.coffee.sku.api.sku.req.SkuListReq;
import com.coffee.sku.service.inquire.bean.InquireSalesInformation;
import com.coffee.sku.service.inquire.bean.InquireSkuContent;
import com.coffee.sku.service.inquire.bean.InquireSkuTestingMessage;
import com.coffee.sku.service.inquire.dao.InquireSalesInformationDao;
import com.coffee.sku.service.inquire.dao.InquireSkuContentDao;
import com.coffee.sku.service.inquire.dao.InquireSkuTestingMessageDao;
import com.coffee.sku.service.sku.bean.Sku;
import com.coffee.sku.service.sku.bean.SkuCategory;
import com.coffee.sku.service.sku.dao.SkuCategoryDao;
import com.coffee.sku.service.sku.dao.SkuDao;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@DubboService(dynamic = true)
public class InquireSkuContentServiceImpl implements InquireSkuContentService {
    private final static Logger logger = LoggerFactory.getLogger(InquireSkuContentServiceImpl.class);
    @Resource
    private InquireSkuContentDao inquireSkuContentDao;
    @Resource
    private SkuCategoryDao skuCategoryDao;
    @Resource
    private InquireSalesInformationDao inquireSalesInformationDao;
    @Resource
    private InquireSkuTestingMessageDao inquireSkuTestingMessageDao;
    @Resource
    private SkuDao skuDao;
    @Resource
    private PlatformBaseDeployDao platformBaseDeployDao;
    @Resource
    private SupplierDao supplierDao;
    @Override
    public Page<InquireSkuContentResult> getPager(InquireSkuContentQuery param, PagerInfo pr) throws BusinessException {
        Page<InquireSkuContentResult> pager = new Page<InquireSkuContentResult>(pr.getStart(), pr.getPageSize());
        Integer count = inquireSkuContentDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<InquireSkuContent> list = inquireSkuContentDao.page(param, pr.getStart(), pr.getPageSize());
            List<InquireSkuContentResult> inquireSkuContentResults = DTOUtils.convertList(list, InquireSkuContentResult.class);
            for (InquireSkuContentResult inquireSkuContentResult : inquireSkuContentResults) {
                InquireSalesInformationQuery inquireSalesInformationQuery = new InquireSalesInformationQuery();
                inquireSalesInformationQuery.setSkuId(inquireSkuContentResult.getSkuId());
                inquireSalesInformationQuery.setIsDelete("N");
                InquireSalesInformation inquireSalesInformation = inquireSalesInformationDao.findFirst(inquireSalesInformationQuery);
                InquireSalesInformationResult inquireSalesInformationResult = new InquireSalesInformationResult();
                BeanUtilExt.copyProperties(inquireSalesInformationResult, inquireSalesInformation);
                inquireSkuContentResult.setInquireSalesInformationResult(inquireSalesInformationResult);
                if (inquireSkuContentResult.getOriginPrice() != null) {
                    PlatformBaseDeploy platformBaseDeployDaoById = platformBaseDeployDao.findById(Integer.parseInt(inquireSkuContentResult.getOriginPrice()));
                    if (platformBaseDeployDaoById != null) {
                        inquireSkuContentResult.setOriginPrice(platformBaseDeployDaoById.getName());
                    }
                }
                if (inquireSkuContentResult.getLevel() != null) {
                    PlatformBaseDeploy platformBaseDeployDaoById = platformBaseDeployDao.findById(Integer.parseInt(inquireSkuContentResult.getLevel()));
                    if (platformBaseDeployDaoById != null) {
                        inquireSkuContentResult.setLevel(platformBaseDeployDaoById.getName());
                    }
                }
                Supplier supplier = supplierDao.findById(inquireSkuContentResult.getSupplierId());
                if (supplier!=null){
                    inquireSkuContentResult.setBusinessName(supplier.getBusinessName());
                    inquireSkuContentResult.setSupplierName(supplier.getName());
                }
            }

            pager.setData(inquireSkuContentResults);

        }
        return pager;
    }

    @Override
    public Integer count(InquireSkuContentQuery param) {
        Integer count = inquireSkuContentDao.count(param);
        return count;
    }

    @Override
    public InquireSkuContentResult getDetail(Integer id, Integer supplierId) throws BusinessException {
        InquireSkuContentResult inquireSkuContentResult = inquireSkuContentDao.findBySkuId(id);
        if (inquireSkuContentResult == null) {
            throw new BusinessException(ExceptionEnum.ERROR_1328.getCode(), ExceptionEnum.ERROR_1328.getMessage());
        }
        if (supplierId != null) {
            if (!inquireSkuContentResult.getSupplierId().equals(supplierId)) {
                throw new BusinessException(ExceptionEnum.ERROR_1342.getCode(), ExceptionEnum.ERROR_1342.getMessage());
            }
        }
        ValidateBusinessException.assertTrue(SkuTypeEnum.INQUIRE.getCode().equals(inquireSkuContentResult.getSkuType()), ExceptionEnum.ERROR_1328);
        InquireSalesInformationQuery inquireSalesInformationQuery = new InquireSalesInformationQuery();
        inquireSalesInformationQuery.setSkuId(inquireSkuContentResult.getSkuId());
        inquireSalesInformationQuery.setIsDelete("N");
        InquireSalesInformation inquireSalesInformation = inquireSalesInformationDao.findFirst(inquireSalesInformationQuery);
        InquireSalesInformationResult inquireSalesInformationResult = new InquireSalesInformationResult();
        BeanUtilExt.copyProperties(inquireSalesInformationResult, inquireSalesInformation);
        inquireSkuContentResult.setInquireSalesInformationResult(inquireSalesInformationResult);
        InquireSkuTestingMessageQuery inquireSkuTestingMessageQuery = new InquireSkuTestingMessageQuery();
        inquireSkuTestingMessageQuery.setInquireSkuId(inquireSkuContentResult.getId());
        List<InquireSkuTestingMessage> inquireSkuTestingMessages = inquireSkuTestingMessageDao.list(inquireSkuTestingMessageQuery);
        inquireSkuContentResult.setInquireSkuTestingMessageResults(DTOUtils.convertList(inquireSkuTestingMessages, InquireSkuTestingMessageResult.class));
        String categoryName = "";
        List<Integer> categoryIds=new ArrayList<>();
        List<String> categoryNames=new ArrayList<>();
        if (StringUtils.isNotEmpty(inquireSkuContentResult.getPidPath())){
            String pidPath = inquireSkuContentResult.getPidPath();
            String[] split = pidPath.split(",");
            for (String s : split) {
                int i = Integer.parseInt(s);
                categoryIds.add(i);
                SkuCategory skuCategory = skuCategoryDao.findById(i);
                if (skuCategory != null) {
                    categoryName = categoryName + "/" + skuCategory.getName();
                    categoryNames.add(skuCategory.getName());
                }
            }
            String substring = categoryName.substring(1, categoryName.length());
            inquireSkuContentResult.setCategoryName(substring);
        }
        inquireSkuContentResult.setCategoryIds(categoryIds);
        inquireSkuContentResult.setCategoryNames(categoryNames);
        return inquireSkuContentResult;
    }

    @Override
    public InquireSkuContentResult getMerchantDetail(Integer skuId) throws BusinessException {
        InquireSkuContentResult inquireSkuContentResult = inquireSkuContentDao.findBySkuId(skuId);
        if (inquireSkuContentResult == null) {
            throw new BusinessException(ExceptionEnum.ERROR_1328.getCode(), ExceptionEnum.ERROR_1328.getMessage());
        }
        ValidateBusinessException.assertTrue(SkuTypeEnum.INQUIRE.getCode().equals(inquireSkuContentResult.getSkuType()), ExceptionEnum.ERROR_1328);
        InquireSalesInformationQuery inquireSalesInformationQuery = new InquireSalesInformationQuery();
        inquireSalesInformationQuery.setSkuId(inquireSkuContentResult.getSkuId());
        inquireSalesInformationQuery.setIsDelete("N");
        InquireSalesInformation inquireSalesInformation = inquireSalesInformationDao.findFirst(inquireSalesInformationQuery);
        InquireSalesInformationResult inquireSalesInformationResult = new InquireSalesInformationResult();
        BeanUtilExt.copyProperties(inquireSalesInformationResult, inquireSalesInformation);
        inquireSkuContentResult.setInquireSalesInformationResult(inquireSalesInformationResult);
        Supplier supplier = supplierDao.findById(inquireSkuContentResult.getSupplierId());
        inquireSkuContentResult.setSupplierName(supplier.getName());
        inquireSkuContentResult.setBusinessName(supplier.getBusinessName());
        if (StringUtils.isNotEmpty(inquireSkuContentResult.getLevel())){
            PlatformBaseDeploy platformBaseDeploy = platformBaseDeployDao.findById(Integer.parseInt(inquireSkuContentResult.getLevel()));
            if (platformBaseDeploy!=null){
                inquireSkuContentResult.setLevel(platformBaseDeploy.getName());
            }
        }
        return inquireSkuContentResult;
    }

    @Override
    public List<InquireSkuContentResult> getList(InquireSkuContentQuery param, PagerInfo pr) {
        List<InquireSkuContent> list = inquireSkuContentDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, InquireSkuContentResult.class);
    }

    @Override
    public List<InquireSkuContentResult> getList(InquireSkuContentQuery param) {
        List<InquireSkuContent> list = inquireSkuContentDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, InquireSkuContentResult.class);
    }

    @Override
    public List<InquireSkuContentResult> getList() {
        InquireSkuContentQuery param = new InquireSkuContentQuery();
        List<InquireSkuContent> list = inquireSkuContentDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, InquireSkuContentResult.class);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) throws BusinessException {
        Sku sku = skuDao.findById(id);
        ValidateBusinessException.assertNonNull(sku, ExceptionEnum.ERROR_1318);
        InquireSkuContent inquireSkuContent = inquireSkuContentDao.findInquireSkuContentBySkuId(id);
        ValidateBusinessException.assertNonNull(inquireSkuContent, ExceptionEnum.ERROR_1332);
        ValidateBusinessException.assertFalse("Y".equals(sku.getIsEnable()), ExceptionEnum.ERROR_1333);
        skuDao.deleteById(id);
        inquireSkuContentDao.deleteById(inquireSkuContent.getId());
        inquireSalesInformationDao.deleteBySkuId(id);
        inquireSkuTestingMessageDao.deleteByInquireSkuIdId(inquireSkuContent.getId());
    }

    @Override
    public void addOrUpdate(InquireSkuContentResult result) throws BusinessException {
        InquireSkuContent item = new InquireSkuContent();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            item.setCreateTime(new Date());
            inquireSkuContentDao.insert(item);
        } else {
            InquireSkuContent tmp = inquireSkuContentDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            inquireSkuContentDao.updateById(item);
        }
    }

    @Override
    public Map<Integer, InquireSkuContentResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, InquireSkuContentResult> map = new HashMap<Integer, InquireSkuContentResult>();
        List<InquireSkuContent> list = inquireSkuContentDao.findByIds(StringUtil.setToList(idSet));
        List<InquireSkuContentResult> resultList = DTOUtils.convertList(list, InquireSkuContentResult.class);
        for (InquireSkuContentResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, InquireSkuContentResult> getItemMap() {
        Map<Integer, InquireSkuContentResult> map = new HashMap<Integer, InquireSkuContentResult>();
        InquireSkuContentQuery param = new InquireSkuContentQuery();
        List<InquireSkuContent> list = inquireSkuContentDao.page(param, 0, Integer.MAX_VALUE);
        List<InquireSkuContentResult> resultList = DTOUtils.convertList(list, InquireSkuContentResult.class);
        for (InquireSkuContentResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Page<InquireSkuListRep> findList(SkuListReq param, PagerInfo pr) {
        Page<InquireSkuListRep> pager = new Page<InquireSkuListRep>(pr.getStart(), pr.getPageSize());
        logger.info("InquireSkuListRep====" + JSONObject.toJSONString(param));
        Integer count = inquireSkuContentDao.skuCount(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<InquireSkuListRep> inquireSkuListReps = inquireSkuContentDao.skuPage(param, pr.getStart(), pr.getPageSize());
            for (InquireSkuListRep skuListRep : inquireSkuListReps) {
                if (StringUtils.isNotEmpty(skuListRep.getPidPath())){
                    String[] split = skuListRep.getPidPath().split(",");
                    SkuCategory skuCategory = skuCategoryDao.findById(Integer.parseInt(split[0]));
                    if (skuCategory != null) {
                        skuListRep.setCategoryName(skuCategory.getName());
                    }
                }
                InquireSalesInformationQuery skuSalesInformationQuery = new InquireSalesInformationQuery();
                skuSalesInformationQuery.setSkuId(skuListRep.getSkuId());
                skuSalesInformationQuery.setIsDelete("N");
                InquireSalesInformation inquireSalesInformation = inquireSalesInformationDao.findFirst(skuSalesInformationQuery);
                InquireSalesInformationResult inquireSalesInformationResult = new InquireSalesInformationResult();
                BeanUtilExt.copyProperties(inquireSalesInformationResult, inquireSalesInformation);
                skuListRep.setSkuSalesInformationResult(inquireSalesInformationResult);
            }
            pager.setData(inquireSkuListReps);
        }
        return pager;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateInquireSku(InquireSkuUpdateReq skuUpdateReq) throws BusinessException {
        Integer skuId = skuUpdateReq.getSkuId();
        ValidateBusinessException.assertStringNotBlank(skuUpdateReq.getTitle(), ExceptionEnum.ERROR_1319);
        ValidateBusinessException.assertStringNotBlank(skuUpdateReq.getSubtitle(), ExceptionEnum.ERROR_1320);
        ValidateBusinessException.assertIdNotNull(skuUpdateReq.getSupplierId(), ExceptionEnum.ERROR_1321);
        ValidateBusinessException.assertIdNotNull(skuUpdateReq.getSampleCount(), ExceptionEnum.ERROR_1335);
        ValidateBusinessException.assertStringNotBlank(skuUpdateReq.getSampleUnit(), ExceptionEnum.ERROR_1335);
        ValidateBusinessException.assertStringNotBlank(skuUpdateReq.getSampleLogistics(), ExceptionEnum.ERROR_1336);
        ValidateBusinessException.assertStringNotBlank(skuUpdateReq.getCover(), ExceptionEnum.ERROR_1324);
        ValidateBusinessException.assertStringNotBlank(skuUpdateReq.getDetails(), ExceptionEnum.ERROR_1325);
        ValidateBusinessException.assertStringNotBlank(skuUpdateReq.getLogistics(), ExceptionEnum.ERROR_1338);
        ValidateBusinessException.assertCollectionNotEmpty(skuUpdateReq.getInquireSkuTestingMessageResults(), ExceptionEnum.ERROR_1337);
        InquireSalesInformationReq inquireSalesInformationResult = skuUpdateReq.getInquireSalesInformationResult();
        ValidateBusinessException.assertNonNull(inquireSalesInformationResult, ExceptionEnum.ERROR_1326);
        ValidateBusinessException.assertTrue(skuUpdateReq.getSampleCount() >= 0, ExceptionEnum.ERROR_1339);
        ValidateBusinessException.assertTrue(inquireSalesInformationResult.getStock() >= 0, ExceptionEnum.ERROR_1339);
        List<InquireSkuTestingMessageReq> inquireSkuTestingMessageResults = skuUpdateReq.getInquireSkuTestingMessageResults();
        Sku sku = skuDao.findById(skuId);
        ValidateBusinessException.assertNonNull(sku, ExceptionEnum.ERROR_1318);
        ValidateBusinessException.assertFalse("Y".equals(sku.getIsEnable()), ExceptionEnum.ERROR_1334);
        String isApprove = sku.getIsApprove();
        sku = new Sku();
        sku.setId(skuId);
        sku.setSkuType(SkuTypeEnum.INQUIRE.getCode());
        sku.setTitle(skuUpdateReq.getTitle());
        sku.setSubTitle(skuUpdateReq.getSubtitle());
        sku.setSupplierId(skuUpdateReq.getSupplierId());
        if ("N".equals(isApprove)) {
            sku.setIsApprove("W");
        }
        sku.setCategoryId(skuUpdateReq.getCategoryId());
        skuDao.updateById(sku);
        InquireSkuContent inquireSkuContent = inquireSkuContentDao.findInquireSkuContentBySkuId(skuId);
        ValidateBusinessException.assertNonNull(inquireSkuContent, ExceptionEnum.ERROR_1331);
        InquireSkuContent skuContentUpdate = new InquireSkuContent();
        BeanUtils.copyProperties(skuContentUpdate, skuUpdateReq);
        skuContentUpdate.setId(inquireSkuContent.getId());
        skuContentUpdate.setGoodsAttribute(JSONObject.toJSONString(skuUpdateReq.getGoodsAttribute()));
        skuContentUpdate.setOtherAttribute(JSONObject.toJSONString(skuUpdateReq.getOtherAttribute()));
        if (skuUpdateReq.getOriginPrice() != null) {
            skuContentUpdate.setOriginPrice(skuUpdateReq.getOriginPrice() + "");
        }
        if (skuUpdateReq.getLevel() != null) {
            skuContentUpdate.setLevel(skuUpdateReq.getLevel() + "");
        }
        inquireSkuContentDao.updateById(skuContentUpdate);
        InquireSalesInformation inquireSalesInformation = new InquireSalesInformation();
        inquireSalesInformation.setId(inquireSalesInformationResult.getId());
        inquireSalesInformation.setProductName(inquireSalesInformationResult.getProductName());
        inquireSalesInformation.setSpecifications(inquireSalesInformationResult.getSpecifications());
        inquireSalesInformation.setStock(inquireSalesInformationResult.getStock());
        inquireSalesInformation.setUnit(inquireSalesInformationResult.getUnit());
        inquireSalesInformationDao.updateById(inquireSalesInformation);
        //删除检测信息
        List<Integer> inquireSkuTestingIds = inquireSkuTestingMessageDao.findIdsBySkuId(inquireSkuContent.getId());
        List<Integer> inquireSkuTestingMessageReqIds = inquireSkuTestingMessageResults.stream().filter(inquireSkuTestingMessageReq -> inquireSkuTestingMessageReq.getInquireSkuTestingMessageId() != null).map(InquireSkuTestingMessageReq::getInquireSkuTestingMessageId).collect(Collectors.toList());
        Collection testSubtract = CollectionUtils.subtract(inquireSkuTestingIds, inquireSkuTestingMessageReqIds);
        List<Integer> testSubtractList = new ArrayList<>(testSubtract);
        inquireSkuTestingMessageDao.deleteByIds(testSubtractList);
        ValidateBusinessException.assertTrue(inquireSkuTestingIds.containsAll(inquireSkuTestingMessageReqIds), ExceptionEnum.ERROR_1329);
        //添加新增的检测信息
        List<InquireSkuTestingMessageReq> addTest = inquireSkuTestingMessageResults.stream().filter(inquireSkuTestingMessageReq -> inquireSkuTestingMessageReq.getInquireSkuTestingMessageId() == null).collect(Collectors.toList());
        if (addTest != null && addTest.size() > 0) {
            insertTestingMessage(addTest, inquireSkuContent.getId());
        }
        //修改检测信息
        List<InquireSkuTestingMessageReq> updateTest = inquireSkuTestingMessageResults.stream().filter(inquireSkuTestingMessageReq -> inquireSkuTestingMessageReq.getInquireSkuTestingMessageId() != null).collect(Collectors.toList());
        if (updateTest != null && updateTest.size() > 0) {
            updateInquireSkuTestingMessage(updateTest, inquireSkuContent.getId());
        }
    }

    public void updateInquireSkuTestingMessage(List<InquireSkuTestingMessageReq> inquireSkuTestingMessageReqs, Integer inquireSkuId) {
        for (InquireSkuTestingMessageReq inquireSkuTestingMessageReq : inquireSkuTestingMessageReqs) {
            ValidateBusinessException.assertTrue(inquireSkuTestingMessageReq.getTestingPrice() >= 0, ExceptionEnum.ERROR_1340);
            InquireSkuTestingMessage inquireSkuTestingMessage = new InquireSkuTestingMessage();
            inquireSkuTestingMessage.setId(inquireSkuTestingMessageReq.getInquireSkuTestingMessageId());
            inquireSkuTestingMessage.setInquireSkuId(inquireSkuId);
            inquireSkuTestingMessage.setTestingCompany(inquireSkuTestingMessageReq.getTestingCompany());
            inquireSkuTestingMessage.setIsFree(inquireSkuTestingMessageReq.getIsFree());
            inquireSkuTestingMessage.setTestingPrice(inquireSkuTestingMessageReq.getTestingPrice());
            inquireSkuTestingMessage.setTestProof(inquireSkuTestingMessageReq.getTestProof());
            inquireSkuTestingMessage.setCreateTime(new Date());
            inquireSkuTestingMessageDao.updateById(inquireSkuTestingMessage);
        }
    }

    public void insertTestingMessage(List<InquireSkuTestingMessageReq> inquireSkuTestingMessageReqs, Integer inquireSkuId) {
        for (InquireSkuTestingMessageReq testingMessageReq : inquireSkuTestingMessageReqs) {
            ValidateBusinessException.assertTrue(testingMessageReq.getTestingPrice() >= 0, ExceptionEnum.ERROR_1340);
            InquireSkuTestingMessage inquireSkuTestingMessage = new InquireSkuTestingMessage();
            inquireSkuTestingMessage.setInquireSkuId(inquireSkuId);
            inquireSkuTestingMessage.setTestingCompany(testingMessageReq.getTestingCompany());
            inquireSkuTestingMessage.setIsFree(testingMessageReq.getIsFree());
            inquireSkuTestingMessage.setTestingPrice(testingMessageReq.getTestingPrice());
            inquireSkuTestingMessage.setTestProof(testingMessageReq.getTestProof());
            inquireSkuTestingMessage.setCreateTime(new Date());
            inquireSkuTestingMessageDao.insert(inquireSkuTestingMessage);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addInquireSku(InquireSkuAddReq inquireSkuAddReq) throws BusinessException {
        ValidateBusinessException.assertStringNotBlank(inquireSkuAddReq.getTitle(), ExceptionEnum.ERROR_1319);
        ValidateBusinessException.assertStringNotBlank(inquireSkuAddReq.getSubtitle(), ExceptionEnum.ERROR_1320);
        ValidateBusinessException.assertIdNotNull(inquireSkuAddReq.getSupplierId(), ExceptionEnum.ERROR_1321);
        ValidateBusinessException.assertIdNotNull(inquireSkuAddReq.getCategoryId(), ExceptionEnum.ERROR_1322);
        ValidateBusinessException.assertIdNotNull(inquireSkuAddReq.getSampleCount(), ExceptionEnum.ERROR_1335);
        ValidateBusinessException.assertStringNotBlank(inquireSkuAddReq.getSampleUnit(), ExceptionEnum.ERROR_1335);
        ValidateBusinessException.assertStringNotBlank(inquireSkuAddReq.getSampleLogistics(), ExceptionEnum.ERROR_1336);
        ValidateBusinessException.assertStringNotBlank(inquireSkuAddReq.getCover(), ExceptionEnum.ERROR_1324);
        ValidateBusinessException.assertStringNotBlank(inquireSkuAddReq.getDetails(), ExceptionEnum.ERROR_1325);
        ValidateBusinessException.assertStringNotBlank(inquireSkuAddReq.getLogistics(), ExceptionEnum.ERROR_1338);
        ValidateBusinessException.assertCollectionNotEmpty(inquireSkuAddReq.getInquireSkuTestingMessageResults(), ExceptionEnum.ERROR_1337);
        InquireSalesInformationReq inquireSalesInformationResult = inquireSkuAddReq.getInquireSalesInformationResult();
        ValidateBusinessException.assertNonNull(inquireSalesInformationResult, ExceptionEnum.ERROR_1326);
        ValidateBusinessException.assertTrue(inquireSkuAddReq.getSampleCount() >= 0, ExceptionEnum.ERROR_1339);
        ValidateBusinessException.assertTrue(inquireSalesInformationResult.getStock() >= 0, ExceptionEnum.ERROR_1339);
        ValidateBusinessException.assertTrue(Integer.parseInt(inquireSalesInformationResult.getSpecifications()) >= 0, ExceptionEnum.ERROR_1341);

        Sku sku = new Sku();
        sku.setTitle(inquireSkuAddReq.getTitle());
        sku.setSubTitle(inquireSkuAddReq.getSubtitle());
        sku.setSupplierId(inquireSkuAddReq.getSupplierId());
        sku.setCategoryId(inquireSkuAddReq.getCategoryId());
        sku.setIsEnable("N");
        sku.setIsApprove("W");
        sku.setCreateTime(new Date());
        sku.setIsDelete("N");
        sku.setSkuType(SkuTypeEnum.INQUIRE.getCode());
        skuDao.insert(sku);

        InquireSkuContent inquireSkuContent = new InquireSkuContent();
        BeanUtils.copyProperties(inquireSkuContent, inquireSkuAddReq);
        inquireSkuContent.setSkuId(sku.getId());
        inquireSkuContent.setCreateTime(new Date());
        inquireSkuContent.setGoodsAttribute(JSONObject.toJSONString(inquireSkuAddReq.getGoodsAttribute()));
        inquireSkuContent.setOtherAttribute(JSONObject.toJSONString(inquireSkuAddReq.getOtherAttribute()));
        if (inquireSkuAddReq.getOriginPrice() != null) {
            inquireSkuContent.setOriginPrice(inquireSkuAddReq.getOriginPrice() + "");
        }
        if (inquireSkuAddReq.getLevel() != null) {
            inquireSkuContent.setLevel(inquireSkuAddReq.getLevel() + "");
        }
        inquireSkuContentDao.insert(inquireSkuContent);
        InquireSalesInformation inquireSalesInformation = new InquireSalesInformation();
        BeanUtilExt.copyProperties(inquireSalesInformation, inquireSalesInformationResult);
        inquireSalesInformation.setSkuId(sku.getId());
        inquireSalesInformation.setCreateTime(new Date());
        inquireSalesInformationDao.insert(inquireSalesInformation);
        List<InquireSkuTestingMessageReq> inquireSkuTestingMessageResults = inquireSkuAddReq.getInquireSkuTestingMessageResults();
        if (inquireSkuTestingMessageResults != null && inquireSkuTestingMessageResults.size() > 0) {
            for (InquireSkuTestingMessageReq inquireSkuTestingMessageResult : inquireSkuTestingMessageResults) {
                ValidateBusinessException.assertTrue(inquireSkuTestingMessageResult.getTestingPrice() >= 0, ExceptionEnum.ERROR_1340);
                InquireSkuTestingMessage inquireSkuTestingMessage = new InquireSkuTestingMessage();
                BeanUtilExt.copyProperties(inquireSkuTestingMessage, inquireSkuTestingMessageResult);
                inquireSkuTestingMessage.setInquireSkuId(inquireSkuContent.getId());
                inquireSkuTestingMessage.setCreateTime(new Date());
                inquireSkuTestingMessageDao.insert(inquireSkuTestingMessage);
            }

        }
    }


}
